/*
 * @Description:
 * @Author: wangqi
 * @Date: 2022-03-10 17:48:12
 * @LastEditTime: 2022-04-29 18:09:53
 */
import 'nprogress/css/nprogress.css'; // 进度条样式
import { type App } from 'vue';
import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router';
import outsideLayout from './outsideLayout';
import { whiteNameList } from './constant';

// 路由守卫
import { createRouterGuards } from './routerGuards';

export const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'Layout',
    redirect: '/dashboard/index',
    meta: { title: 'One Pice' },
    component: () => import(/* webpackChunkName: "layout" */ '@/layout/index.vue'),
    children: [],
  },
  ...outsideLayout,
];

export const router = createRouter({
  history: createWebHashHistory(),
  routes,
  scrollBehavior(to, from, savedPosition) {
    console.log(to, from, 'x');
    if (savedPosition) {
      return savedPosition;
    } else {
      return { top: 0 };
    }
  },
});

// 重置路由
export function resetRouter() {
  router.getRoutes().forEach((route) => {
    const { name } = route;
    const isHaveWhiteName = whiteNameList.some((val) => val === name);
    if (name && !isHaveWhiteName) {
      router.hasRoute(name) && router.removeRoute(name);
    }
  });
}

export async function setupRouter(app: App) {
  // 创建路由守卫
  createRouterGuards(router, whiteNameList);
  app.use(router);
  await router.isReady();
}

export default router;
